package main

import (
	"fmt"
	"gitee.com/hakwolf/taibak/internal/models"
	"gitee.com/hakwolf/taibak/internal/routers/dhost"
	"gitee.com/hakwolf/taibak/internal/routers/task"
	"github.com/ying32/govcl/vcl"
	"github.com/ying32/govcl/vcl/types"
	"strconv"
	"strings"
)

var (
	// 是否登录
	isLogin bool
)

type SqlserverOATH int

const (
	_SqlserverOATH = iota
	DSqlNO
	DSqlWindow
	DSQLSQL
)

// ::private::
type TMainFormFields struct {
}

func (f *TMainForm) OnSpeedButton3Click(sender vcl.IObject) {

}

func (f *TMainForm) OnSpeedButton4Click(sender vcl.IObject) {

}

func (f *TMainForm) OnSButtonNewClick(sender vcl.IObject) {
	//panel111 :=  NewFmShilipanel(f)
	//panel111.SetParent(f.PanelShili)
	//panel111.SetAlign(types.AlTop)
	//panel111.Show()
	ShiliForm := NewFmShiliForm(f)
	if ShiliForm.ShowModal() == types.MrOk {
		datetype := strconv.Itoa(int(ShiliForm.CBShiLiType.ItemIndex()))
		local := int16(ShiliForm.CBAuthentication.ItemIndex())
		port := int64(ShiliForm.SpinEdit1.Value())
		hostForm := dhost.DHostForm{0, ShiliForm.EDShiLiname.Text(), ShiliForm.EDHost.Text(), port,
			"", datetype, ShiliForm.EdUser.Text(), ShiliForm.Edpass.Text(),
			"", "", "", "", "", local}
		dhost.UIStore(hostForm)
	}
	ShiliForm.Free()

}

func (f *TMainForm) OnFormCreate(sender vcl.IObject) {
	models.Db = models.CreateSqliteDb()
	hosts := dhost.UIAll()
	for _, host := range hosts {
		panel111 := NewFmShilipanel(f)
		panel111.SetParent(f.PanelShili)
		panel111.SetAlign(types.AlTop) //
		panel111.Label1.SetCaption(host.Databasetype)
		//panel111.Label2.SetCaption(host.Name)  //本地或者远程
		panel111.Label3.SetCaption(host.Name)
		panel111.MainID = host.Id
		panel111.Show()
	}

	tasks := task.UIAll()
	for _, task := range tasks {
		panel999 := NewFmTaskPanel(f)
		panel999.SetParent(f.PanelTask)
		hostModel := new(models.DHost)
		hostModel.Find(task.DhostId)
		panel999.FmShiLiName.SetCaption(hostModel.Name)
		panel999.TaskName.SetCaption(task.Name)
		panel999.Show()
		// panel111.Show()
	}

}

func (f *TMainForm) OnFormShow(sender vcl.IObject) {
	f.SetCaption("钛备份-管理")

	f.clearHost()

	f.clearTask()
}

func (f *TMainForm) clearTask() {
	itemC := MainForm.ScrollBoxTask.ControlCount()
	for i := int(itemC) - 1; i > -1; i-- {
		//if f.ScrollBoxTask.Components(int32(i)).InheritsFrom(){
		//
		//}
		fmt.Println("shilipanelName=" + vcl.AsForm(f.ScrollBoxTask.Controls(int32(i))).Name())
		vcl.AsForm(f.ScrollBoxTask.Controls(int32(i))).SetVisible(false)
		//vcl.AsForm(f.ScrollBoxTask.Controls(int32(i))).Free()   //free后包莫名其妙的事情 如开启大√提示启动当前任务
	}
}

func (f *TMainForm) clearHost() {
	itemC := MainForm.ScrollBoxShiLi.ControlCount()
	for i := int(itemC) - 1; i > -1; i-- {
		//if f.ScrollBoxTask.Components(int32(i)).InheritsFrom(){
		//
		//}
		fmt.Println("hostpanelName=" + vcl.AsForm(f.ScrollBoxShiLi.Controls(int32(i))).Name())
		vcl.AsForm(f.ScrollBoxShiLi.Controls(int32(i))).SetVisible(false)
		//vcl.AsForm(f.ScrollBoxShiLi.Controls(int32(i))).Free()   //free后包莫名其妙的事情 如开启大√提示启动当前任务
	}
}

func (f *TMainForm) OnSTaskButtonNewClick(sender vcl.IObject) {
	FmTaskEdit = NewFmTaskEdit(f)
	FmTaskEdit.Init()
	FmTaskEdit.SetModalResult(types.MrNone)
	if FmTaskEdit.ShowModal() == types.MrOk {
		interPd := FmTaskEdit.SEDInterPd.Value()
		specToTalStr := ""
		if FmTaskEdit.RBMinute.Checked() {
			specStr1 := "0 0/"
			specStr2 := strconv.Itoa(int(interPd))
			specStr3 := " * * * ?"
			specToTalStr = fmt.Sprintf("%s%s%s", specStr1, specStr2, specStr3)
		} else if FmTaskEdit.RBHour.Checked() {
			specStr1 := "0 0 0/"
			specStr2 := strconv.Itoa(int(interPd))
			specStr3 := " * * ?"
			specToTalStr = fmt.Sprintf("%s%s%s", specStr1, specStr2, specStr3)

		} else if FmTaskEdit.RBDay.Checked() {
			specStr1 := "0 0 0 1/"
			specStr2 := strconv.Itoa(int(interPd))
			specStr3 := " * ?"
			specToTalStr = fmt.Sprintf("%s%s%s", specStr1, specStr2, specStr3) //注意 Sprint与Sprintf的区别
		} else if FmTaskEdit.RBWeek.Checked() {
			specStr1 := "0 0 0 ? * "
			specStr2 := ""
			if FmTaskEdit.CheckWeek1.Checked() {
				if specStr2 == "" {
					specStr2 = "1"
				}
			}
			if FmTaskEdit.CheckWeek2.Checked() {
				if specStr2 == "" {
					specStr2 = "2"
				} else {
					specStr2 = specStr2 + ",2"
				}
			}
			if FmTaskEdit.CheckWeek3.Checked() {
				if specStr2 == "" {
					specStr2 = "3"
				} else {
					specStr2 = specStr2 + ",3"
				}
			}
			if FmTaskEdit.CheckWeek4.Checked() {
				if specStr2 == "" {
					specStr2 = "4"
				} else {
					specStr2 = specStr2 + ",4"
				}
			}
			if FmTaskEdit.CheckWeek5.Checked() {
				if specStr2 == "" {
					specStr2 = "5"
				} else {
					specStr2 = specStr2 + ",5"
				}
			}
			if FmTaskEdit.CheckWeek6.Checked() {
				if specStr2 == "" {
					specStr2 = "6"
				} else {
					specStr2 = specStr2 + ",6"
				}
			}
			if FmTaskEdit.CheckWeek7.Checked() {
				if specStr2 == "" {
					specStr2 = "7"
				} else {
					specStr2 = specStr2 + ",7"
				}
			}
			specToTalStr = fmt.Sprintf("%s%s", specStr1, specStr2)
		}

		fmt.Println(specToTalStr)

		shiLiname := strings.TrimSpace(FmTaskEdit.EDShiLiname.Text())
		dHostId := taskhosts[FmTaskEdit.CBShiLi.ItemIndex()-1].Id
		protocolId := FmTaskEdit.CBExEType.ItemIndex()
		//datetype := strconv.Itoa(int(FmTaskEdit.CBShiTp.ItemIndex()))
		// port := int64(ShiliForm.SpinEdit1.Value())
		taskForm := task.TaskForm{0, 1, 1, "0",
			shiLiname, specToTalStr, models.TaskProtocol(protocolId), "", 1, 0,
			5, 1, 1000, "", int(dHostId), "", "",
			1, 1, "", "", 1}
		task.UIStore(taskForm)
	}

}

func (f *TMainForm) OnBTUserModfyClick(sender vcl.IObject) {
	fmModpass := NewFmModpass(f)
	//fmModpass.LBUser.SetCaption()
	if fmModpass.ShowModal() == types.MrOk {
		if fmModpass.EDNewPas.Text() != fmModpass.EDConfirmNewPas.Text() {
			vcl.ShowMessage("新密码跟确认密码不一致！")
		}

		var err error
		if err == nil {
			vcl.ShowMessage("修改成功")
		} else {
			vcl.ShowMessage(err.Error())
		}
	}

}

func (f *TMainForm) OnFormClose(sender vcl.IObject, closeAction *types.TCloseAction) {

}

func (f *TMainForm) OnFormCloseQuery(sender vcl.IObject, canClose *bool) {

}

func (f *TMainForm) OnFormDestroy(sender vcl.IObject) {

}

func (f *TMainForm) OnSPBHostRefreshClick(sender vcl.IObject) {

}

func (f *TMainForm) OnSBTIntallEngineClick(sender vcl.IObject) {

}

func (f *TMainForm) OnSBTUnIntallEngineClick(sender vcl.IObject) {

}

func (f *TMainForm) OnSBTStopEngineClick(sender vcl.IObject) {

}

func (f *TMainForm) OnSBStartEngineClick(sender vcl.IObject) {

}

func (f *TMainForm) OnSPBTaskRefreshClick(sender vcl.IObject) {

}

func (f *TMainForm) OnSPBStoreOKClick(sender vcl.IObject) {

}

func (f *TMainForm) OnCbCompressFormatChange(sender vcl.IObject) {

}

func (f *TMainForm) OnCbCompressLevelChange(sender vcl.IObject) {

}

func (f *TMainForm) OnCheckBSetNullpaswClick(sender vcl.IObject) {

}

func (f *TMainForm) OnCheckRenameSoftLogoClick(sender vcl.IObject) {

}

func (f *TMainForm) OnCheckRenameDatabaseTypeClick(sender vcl.IObject) {

}

func (f *TMainForm) OnCheckRenameShiLiNameClick(sender vcl.IObject) {

}

func (f *TMainForm) OnCheckRenameTaskNameClick(sender vcl.IObject) {

}

func (f *TMainForm) OnCheckRenameDatabaseNameClick(sender vcl.IObject) {

}

func (f *TMainForm) OnCheckRenameTableNameClick(sender vcl.IObject) {

}

func (f *TMainForm) OnBtnNetShareAppendClick(sender vcl.IObject) {

}

func (f *TMainForm) OnBtnNetShareDelClick(sender vcl.IObject) {

}

func (f *TMainForm) OnBtnNetShareSaveClick(sender vcl.IObject) {

}

func (f *TMainForm) OnBtnFtpAppendClick(sender vcl.IObject) {

}

func (f *TMainForm) OnBtnFtpSaveClick(sender vcl.IObject) {

}

func (f *TMainForm) OnBtnFtpDelClick(sender vcl.IObject) {

}

func (f *TMainForm) OnBtnFtpTestConnect1Click(sender vcl.IObject) {

}

func (f *TMainForm) OnListViewFtpSelectItem(sender vcl.IObject, item *vcl.TListItem, selected bool) {

}

func (f *TMainForm) OnBtnQiNiuAppendClick(sender vcl.IObject) {

}

func (f *TMainForm) OnBtnQiNiuDelClick(sender vcl.IObject) {

}

func (f *TMainForm) OnBtnQiNiuSaveClick(sender vcl.IObject) {

}

func (f *TMainForm) OnBtnQiNiuTestConnectClick(sender vcl.IObject) {

}

func (f *TMainForm) OnBtnOneDriveAppendClick(sender vcl.IObject) {

}

func (f *TMainForm) OnBtnOneDriveDelClick(sender vcl.IObject) {

}

func (f *TMainForm) OnBtnOneDriveSaveClick(sender vcl.IObject) {

}

func (f *TMainForm) OnBtnOneDriveTestConnectClick(sender vcl.IObject) {

}

func (f *TMainForm) OnBtnBaiduNetdiskAppendClick(sender vcl.IObject) {

}

func (f *TMainForm) OnBtnBaiduNetdiskDelClick(sender vcl.IObject) {

}

func (f *TMainForm) OnBtnBaiduNetdiskSaveClick(sender vcl.IObject) {

}

func (f *TMainForm) OnBtnBaiduNetdiskTestConnectClick(sender vcl.IObject) {

}

func (f *TMainForm) OnBtlogPrevPageClick(sender vcl.IObject) {

}

func (f *TMainForm) OnBtlogNextPageClick(sender vcl.IObject) {

}

func (f *TMainForm) OnBtlogFirstPageClick(sender vcl.IObject) {

}

func (f *TMainForm) OnBtlogDelLogClick(sender vcl.IObject) {

}

func (f *TMainForm) OnBTUserNewClick(sender vcl.IObject) {

}

func (f *TMainForm) OnBTUserDelClick(sender vcl.IObject) {

}

func (f *TMainForm) OnBTUserStoreClick(sender vcl.IObject) {

}

func (f *TMainForm) OnBTUserModfyOtherClick(sender vcl.IObject) {

}

func (f *TMainForm) OnListViewUserSelectItem(sender vcl.IObject, item *vcl.TListItem, selected bool) {

}

func (f *TMainForm) OnSPBNavigFirstPageClick(sender vcl.IObject) {

}

func (f *TMainForm) OnSPBNavigTaskClick(sender vcl.IObject) {

}

func (f *TMainForm) OnSPBNavigHostClick(sender vcl.IObject) {

}

func (f *TMainForm) OnSPBNavigTaskLogClick(sender vcl.IObject) {

}

func (f *TMainForm) OnSPBUserManageClick(sender vcl.IObject) {

}

func (f *TMainForm) OnSPBNavigTotalSetClick(sender vcl.IObject) {

}

func (f *TMainForm) OnSPBRegInfoClick(sender vcl.IObject) {

}
